//
//  StringUtils.java
//  doc-recode
//
//  Created by Eden He on 2013-7-3
//  Copyright (c) 2013 Chunyu.mobi 
//  All rights reserved
//

package com.aj.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.text.TextUtils;

public class StringUtils {

	/**
	 * 判断字符串是否包含空白字符
	 *
	 * @param content
	 * @return
	 */
	public static boolean isContainWhitespaceCharacter(String content) {
		if (content == null) {
			return false;
		}
		Pattern pattern = Pattern.compile("\\s");
		Matcher matcher = pattern.matcher(content);
		return matcher.find();
	}

	/**
	 * 转化字符串到MD5格式
	 *
	 * @param value
	 * @return
	 */
	public static String convertToMD5Format(String value) {
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			digest.update(value.getBytes());
			byte messageDigest[] = digest.digest();
			StringBuilder hexString = new StringBuilder();
			for (byte element : messageDigest) {
				hexString.append(String.format("%02x", 0xFF & element));
			}
			return hexString.toString();
		} catch (Exception e) {
			return value;
		}
	}

	/**
	 * 字符串是否相等 null=""
	 *
	 * @param str1
	 *            , str2
	 * @return
	 */
	public static boolean isEqual(String str1, String str2) {
		boolean isStr1Empty = TextUtils.isEmpty(str1);
		boolean isStr2Empty = TextUtils.isEmpty(str2);

		if (!isStr1Empty && !isStr2Empty) {
			return str1.equals(str2);
		} else {
			return !(isStr1Empty ^ isStr2Empty);
		}
	}

	/**
	 * 将手机号规范化，只最后11位数字
	 *
	 * @param phone
	 * @return
	 */
	public static String normalizePhoneNumber(String phone) {
		Pattern p = Pattern.compile("[^0-9]");
		Matcher m = p.matcher(phone);
		phone = m.replaceAll("");
		if (phone.length() > 11) {
			phone = phone.substring(phone.length() - 11);
		}
		return phone;
	}

	public static boolean isCNChar(char c) {
		return (c >= 0x4e00 && c <= 0x9fa5);
	}

	public static boolean isENChar(char c) {
		if (c >= 'a' && c <= 'z')
			return true;
		if (c >= 'A' && c <= 'Z')
			return true;
		return false;
	}

	public static boolean isNumChar(char c) {
		return (c >= '0' && c <= '9');
	}

	public static String md5(String str) {
		MessageDigest md;
		String dstr = null;
		try {
			md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			dstr = new BigInteger(1, md.digest()).toString(16);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dstr;
	}

	public static boolean isStringEmpty(String string) {
		if (string == null) {
			return true;
		} else {
			if (string.trim().length() == 0) {
				return true;
			} else {
				return false;
			}
		}
	}

	public static int compare(String version1, String version2) {
		String[] subVersions1 = version1.trim().split("\\.");
		String[] subVersions2 = version2.trim().split("\\.");
		int compareLength = Math.min(subVersions1.length, subVersions2.length);
		for (int i = 0; i < compareLength; i++) {
			int subVersion1 = Integer.valueOf(subVersions1[i]);
			int subVersion2 = Integer.valueOf(subVersions2[i]);
			if (subVersion1 < subVersion2) {
				return -1;
			} else if (subVersion1 > subVersion2) {
				return 1;
			}
		}
		if (subVersions1.length == subVersions2.length) {
			return 0;
		} else {
			return (subVersions1.length < subVersions2.length ? -1 : 1);
		}
	}
}
